/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.enac.geoapp.parser.pln;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
 * ContentHandler for the parsing of flight plan xml files. Retrieves the point
 * for the main and alternate flight plans
 * 
 * @author mmagnaudet
 * 
 * 
 */
public class SimpleContentHandler implements ContentHandler {

	/**
	 * @return the mainPlan
	 */
	public List<MapPoint> getMainPlan() {
		return mainPlan;
	}

	/**
	 * @return the altPlan
	 */
	public List<MapPoint> getAltPlan() {
		return altPlan;
	}

	private boolean inAlt;
	private boolean inWaypoint;
	private MapPoint p;
	private String id;
	private Coordinate latitude;
	private Coordinate longitude;
	private List<MapPoint> mainPlan;
	private List<MapPoint> altPlan;

	/**
	 * Constructeur par defaut.
	 */
	public SimpleContentHandler() {
		super();
		// On definit le locator par defaut.
		// locator = new LocatorImpl();
		p = null;
		latitude = null;
		longitude = null;
		mainPlan = new ArrayList<MapPoint>();
		altPlan = new ArrayList<MapPoint>();
		inAlt = false;
		inWaypoint = false;
	}

	/**
	 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
	 */
	public void setDocumentLocator(Locator value) {
	}

	/**
	 * @see org.xml.sax.ContentHandler#startDocument()
	 */
	public void startDocument() throws SAXException {

	}

	/**
	 * @see org.xml.sax.ContentHandler#endDocument()
	 */
	public void endDocument() throws SAXException {
	}

	/**
	 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
	 *      java.lang.String)
	 */
	public void startPrefixMapping(String prefix, String URI)
			throws SAXException {
	}

	/**
	 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
	 */
	public void endPrefixMapping(String prefix) throws SAXException {
	}

	/**
	 * Detecte l'ouverture des balises ALTFlighPlan, Waypoint, Lat et Long Le
	 * cas échéant crée un nouvel objet waypoint
	 * 
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
	 *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String nameSpaceURI, String localName,
			String rawName, Attributes attributs) throws SAXException {

		if (localName.equalsIgnoreCase("ALTFlightPlan")) {
			inAlt = true;
		}
		if (localName.equalsIgnoreCase("waypoint")) {
			inWaypoint = true;
			id = attributs.getValue(0);
		}

		if (inWaypoint && localName.equalsIgnoreCase("lat")) {
			latitude = new Coordinate();
			latitude.setDeg(findAttributeValue(attributs, "deg"));
			latitude.setHundredthMin(findAttributeValue(attributs,
					"hundredthmin"));
			latitude.setMin(findAttributeValue(attributs, "min"));
		}

		if (inWaypoint && localName.equalsIgnoreCase("long")) {
			longitude = new Coordinate();
			longitude.setDeg(findAttributeValue(attributs, "deg"));
			longitude.setHundredthMin(findAttributeValue(attributs,
					"hundredthmin"));
			longitude.setMin(findAttributeValue(attributs, "min"));
		}

	}

	private String findAttributeValue(Attributes attributs, String name) {
		for (int i = 0; i < attributs.getLength(); i++) {
			if (attributs.getLocalName(i).equalsIgnoreCase(name))
				return attributs.getValue(i);
		}
		return null;
	}

	/**
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public void endElement(String nameSpaceURI, String localName, String rawName)
			throws SAXException {

		if (localName.equalsIgnoreCase("coord")) {
			p = MapPointBuilder.buildMapPoint(id, latitude, longitude);

			if (inAlt) {
				altPlan.add(p);
			} else {
				mainPlan.add(p);
			}
			inWaypoint = false;
		}

		if (localName.equalsIgnoreCase("ALTFlightPlan")) {
			inAlt = false;
		}
	}

	/**
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] ch, int start, int end) throws SAXException {
	}

	/**
	 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
	 */
	public void ignorableWhitespace(char[] ch, int start, int end)
			throws SAXException {
	}

	/**
	 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
	 *      java.lang.String)
	 */
	public void processingInstruction(String target, String data)
			throws SAXException {
	}

	/**
	 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
	 */
	public void skippedEntity(String arg0) throws SAXException {
	}

}
